/*

(Page298 11.14 exercises 1)In the following program,
(a) which conversions are used in each expression?
(b) Modify the program so that it will run and print the values of each legal expression.

  struct X{
        int i;
        X(int);
        X operator+(int);
  };

  struct Y{
        int i;
        Y(X);
        Y operator+(X);
        operator int();
  };

  extern X operator*(X,Y);
  extern int f(X);

  X x = 1;
  Y y = x;
  int i = 2;

  int main()
  {
       i+10;       //(1)
       y+10;     //(2)
       y+10*y; //(3)
       x + y + i; //(4)
       x * x + i;  //(5)
       f(7);         //(6)
       f(y);         //(7)
       y+y;       //(8)
       10+y;     //(9)
}

示例：
(1) 两个int（基本类型数据）相加，没有类型转换
*/
#include <iostream>
using namespace std;

struct X{
    int i;
    X(int);
    X operator+(int);
};

X::X(int ii){
    i=ii;
}//由int新建X

X X::operator+(int ii){
    X reX = *this;
    reX.i+=ii;
    return reX;
}//由X+int返回X

//--------分割线---------//

struct Y{
    int i;
    Y(X);
    Y operator+(X);
    explicit operator int();    //只能被显式调用
};

Y::Y(X xx){
    i=xx.i;
}//由X创建Y

Y Y::operator+(X xx){
    Y reY = *this;
    reY.i+=xx.i;
    return reY;
}//Y+X

Y::operator int(){
    return i;
}//Y转int

X operator*(X xx,Y yy){
    X reX = xx;
    reX.i *= yy.i;
    return reX;
}//X*y
 
int f(X xx){
    cout<<"Now in f(); The 'i' ="<<xx.i<<endl;
    return xx.i;
}


//--------分割线---------//
int i =2;
X x = 1;
Y y = Y(x);


int main()
{
    int III = i+10;             //(1)       int+int=int   
    cout<<"(1):"<<III<<endl;

    Y YYY = y+10;               //(2)       (Y+(int->X))=Y    
    cout<<"(2):"<<YYY.i<<endl;
    
    YYY = y+10*y;               //(3)       (int->X)*Y=X;Y+X=Y; 
    cout<<"(3):"<<YYY.i<<endl;

    X XXX = x + (int)y + i;     //(4)       X+(Y->int)=X;X+int=X; 
    cout<<"(4):"<<XXX.i<<endl;

    XXX = x * x + i;            //(5)       X*(X->Y)=X;X+int=X;  
    cout<<"(5):"<<XXX.i<<endl;  

    cout<<"(6):";
    f(7);                       //(6)       int->X; 
    cout<<"(7):";
    f((int)y);                  //(7)       Y->int->X;

    YYY = y+(int)y;             //(8)       Y+(Y->int) = Y
    cout<<"(8):"<<YYY.i<<endl;

    III = 10+(int)y;            //(9)       int+(Y->int)=int;
    cout<<"(9):"<<III<<endl;
    return 0;
}